package com.example.panda.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.panda.HomeClassifyActivity;
import com.example.panda.HomeRecTravelActivity;
import com.example.panda.Item.HomeTravelItem;
import com.example.panda.JoinTeamGridViewActivity;
import com.example.panda.MainActivity;
import com.example.panda.R;
import com.example.panda.StartTeamListViewActivity;
import com.example.panda.adapter.HomePagerAdapter;
import com.example.panda.adapter.HomeTravelRecipeAdapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;



public class HomeFragment extends Fragment {
    //搜索框
    private SearchView homeSearchView;
    private ListView homeListView;
    private String[] mStrings = { "aaaaa", "bbbbbb", "cccccc", "ddddddd" };
    private Button btnStart,btnMe;
    private List<ImageView> mImageViewList;
    int[] images = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
    private ViewPager viewPager;
    LayoutInflater inflater;
    ViewGroup container;
    myHandler mMyHandler = new myHandler();
    public HomeFragment() {
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_home,container,false);
        //List
        homeListView = (ListView)view.findViewById(R.id.lv);
        homeListView.setAdapter(new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_list_item_1, mStrings));
        homeListView.setTextFilterEnabled(true);//设置lv可以被过虑
        //Search
        homeSearchView = (SearchView)view.findViewById(R.id.searchView);
        homeSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(view.getContext(), "您的选择是:" + query, Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    // 清除ListView的过滤
                    homeListView.clearTextFilter();
                } else {
                    // 使用用户输入的内容对ListView的列表项进行过滤
                    homeListView.setFilterText(newText);
                }
                return false;
            }
        });

        final List<HomeTravelItem> list = new ArrayList<>();
        for (int i = 0;i < 5; i++){
            list.add(new HomeTravelItem(R.drawable.img1,R.string.home_travel_title1,R.string.home_travel_text1));
            list.add(new HomeTravelItem(R.drawable.img2,R.string.home_travel_title2,R.string.home_travel_text2));
            list.add(new HomeTravelItem(R.drawable.img3,R.string.home_travel_title3,R.string.home_travel_text3));
            list.add(new HomeTravelItem(R.drawable.img4,R.string.home_travel_title4,R.string.home_travel_text4));
            list.add(new HomeTravelItem(R.drawable.img5,R.string.home_travel_title5,R.string.home_travel_text5));
            list.add(new HomeTravelItem(R.drawable.img6,R.string.home_travel_title6,R.string.home_travel_text6));
            list.add(new HomeTravelItem(R.drawable.img7,R.string.home_travel_title7,R.string.home_travel_text7));
        }
        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.home_recycler);
        HomeTravelRecipeAdapter recipeAdaper = new HomeTravelRecipeAdapter(list,view.getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setNestedScrollingEnabled(true);
        recyclerView.setAdapter(recipeAdaper);
        recipeAdaper.setmOnItemClickListener(new HomeTravelRecipeAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                Intent intent = new Intent(view.getContext(), HomeRecTravelActivity.class);
                startActivity(intent);
            }

            @Override
            public void OnItemLongClickListener(View view, int position) {
                Toast.makeText(view.getContext(),"长按成功",Toast.LENGTH_SHORT).show();
            }
        });

        btnStart = (Button)view.findViewById(R.id.home_team_start);
        btnMe = (Button)view.findViewById(R.id.home_team_me);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(view.getContext(), StartTeamListViewActivity.class);
                startActivity(intent1);
            }
        });
        btnMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(view.getContext(), JoinTeamGridViewActivity.class);
                startActivity(intent2);
            }
        });
        ImageView imageViewF = (ImageView)view.findViewById(R.id.home_food_img);
        ImageView imageViewC = (ImageView)view.findViewById(R.id.home_cool_img);
        ImageView imageViewP = (ImageView)view.findViewById(R.id.home_play_img);
        ImageView imageViewT = (ImageView)view.findViewById(R.id.home_travel_img);

        imageViewF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(view.getContext(), HomeClassifyActivity.class);
                startActivity(intent);
            }
        });
        imageViewC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(view.getContext(), HomeClassifyActivity.class);
                startActivity(intent);
            }
        });
        imageViewP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(view.getContext(), HomeClassifyActivity.class);
                startActivity(intent);
            }
        });
        imageViewT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(view.getContext(), HomeClassifyActivity.class);
                startActivity(intent);
            }
        });
        //这个集合用于装轮播图（Imageview）
        mImageViewList = new ArrayList<>();
        ImageView point;
        //用于确定小圆点的格式 位置
        LinearLayout.LayoutParams layoutParams;
        // images是图片数组，图片数组的大小决定存储imageview集合的大小

        for(int i = 0; i < images.length; i++){
            ImageView imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(images[i]);
            mImageViewList.add(imageView);
            point = new ImageView(getContext());
            //圆点的背景为一个指示器，根据可不可以点击显示不一样
            point.setBackgroundResource(R.drawable.selector_food_carousel);
            layoutParams = new LinearLayout.LayoutParams(35,35);
            //leftMargin是小圆点之间的间距
            if(i != 0){
                layoutParams.leftMargin = 15;
                //使小圆点不能接收触摸事件或点击
                point.setEnabled(false);
            }
            LinearLayout ll_food_carousel = (LinearLayout)view.findViewById(R.id.ll_food_carousel);
            ll_food_carousel.addView(point,layoutParams);
        }
        viewPager = (ViewPager)view.findViewById(R.id.vp_food_guide);
        HomePagerAdapter homePagerAdapter = new HomePagerAdapter();
        viewPager.setAdapter(homePagerAdapter);
        viewPager.setCurrentItem(5000000);
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }
//            其中beforepoint初始值为0或我们实现自动轮播时设置的值，这里主要通过改变圆点的状态来实现圆点变化的效果
            @Override
            public void onPageSelected(int i) {
                LinearLayout ll_food_carousel = (LinearLayout)view.findViewById(R.id.ll_food_carousel);
                int beforepoint = 0;
                int newPosition = i % mImageViewList.size();
                    ll_food_carousel.getChildAt(beforepoint).setEnabled(false);
                    ll_food_carousel.getChildAt(newPosition).setEnabled(true);
                    beforepoint += newPosition;
                ll_food_carousel.getChildAt(beforepoint).setEnabled(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        return  view;
    }
    class myHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            View view = inflater.inflate(R.layout.fragment_home,container,false);
            viewPager = (ViewPager)view.findViewById(R.id.vp_food_guide);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            mMyHandler.sendEmptyMessageDelayed(0,2000);
        }
    }
    @Override
    public void onDestroyView() {
        if(mMyHandler != null){
            mMyHandler.removeMessages(0);
            mMyHandler = null;
            // ll_food_carousel.getChildAt(beforepoint).setEnabled(false);
            Log.i("TAG","handler销毁");
        }
        super.onDestroyView();
    }
}
